from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_http_methods
from .models import RegisterModel
from .forms import RegisterForm, LoginForm
from django.shortcuts import render, redirect, reverse
from django.contrib.auth.models import User
from django.contrib.auth import login, authenticate, logout


# Create your views here.
def index(request):
    return render(request, 'index.html')
def admin_index(request):
    return render(request, 'admin/index.html')
def company_index(request):
    return render(request, 'company/index.html')


@require_http_methods(["GET", "POST"])
def user_login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            selected_role = form.cleaned_data.get('role')
            user = authenticate(request, username=username, password=password)

            if user:
                # 验证用户角色是否匹配
                if selected_role == 'admin' and not user.is_superuser:
                    return render(request, 'login.html', {'error': '您不是管理员用户'})
                elif selected_role == 'company' and not user.is_staff:
                    return render(request, 'login.html', {'error': '您不是企业用户'})
                elif selected_role == 'student' and (user.is_staff or user.is_superuser):
                    return render(request, 'login.html', {'error': '您不是学生用户'})

                login(request, user)
                if user.is_superuser:
                    return redirect('author:admin_index')
                elif user.is_staff:
                    return redirect('author:company_index')
                else:
                    return redirect('author:student_index')
            else:
                return render(request, 'login.html', {'error': '用户名或密码错误'})
        else:
            return render(request, 'login.html', {'error': form.errors})


@require_http_methods(["GET", "POST"])
def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    else:
        form = RegisterForm(request.POST)
        if form.is_valid():
            # 检查表单是否有效
            role = form.cleaned_data.get('role')
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            identify_password = form.cleaned_data.get('identify_password')

            print(f"注册角色值: {role}")  # 调试信息

            # 创建RegisterModel实例
            RegisterModel.objects.create(
                role=role,
                username=username,
                password=password,
                identify_password=identify_password
            )
            user = User.objects.create_user(username=username, password=password)
            if role == 'company':
                user.is_staff = True  # 设置企业用户为管理员
            elif role == 'admin':
                user.is_superuser = True
                print(f"设置超级用户权限: {user.is_superuser}")  # 调试信息
            user.save()
            print(f"保存后的超级用户权限: {user.is_superuser}")  # 调试信息
            return redirect('author:login')
        else:
            print(form.errors)
            return redirect('author:register')


@login_required(login_url='author:login')
def user_logout(request):
    logout(request)
    return redirect('author:index')
